package com.live.spaces.shanboli;

import java.io.BufferedReader;
import java.io.InputStreamReader;



public class FileShare {

	JxtaHelper jh;
	
	FileShare(){
		System.out.println("JXTA P2P Filesharing prototype");
		jh = new JxtaHelper();
		jh.startJxta();
	}
	
	private String prompt()
	{
		System.out.print("p2p>");
		BufferedReader in = new BufferedReader(
                new InputStreamReader( System.in ) );
		String s="";
		try{
			s = in.readLine();
		}catch(Exception e){}
		
		return s;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Config conf = Config.getInstance();
		FileShare fs = new FileShare();
		System.out.println("our files from "+conf.getShareDir()+" are offered to our peer.");
		System.out.println("input \"help\" to see the supported commands.");
		String input;
		while(true)
		{
			input = fs.prompt();
			if (input.equalsIgnoreCase("quit")|| input.equalsIgnoreCase("exit"))
			{
				System.exit(0);
			}
			else if (input.startsWith("chat ")){
				fs.jh.sendMsg("chatmessage: "+input.substring(5));
			}
			else if (input.equals("ls"))
				fs.jh.sendMsg("ls");
			else if (input.startsWith("get ")){
				fs.jh.sendMsg(input);
				fs.jh.receiveFile(conf.getShareDir()+input.substring(4));
			}
			else if (input.startsWith("sharedir ")){
				conf.setShareDir(input.substring(9));
				System.out.println("sharedir set to "+conf.getShareDir());
			}
			else
			{
				System.out.println("supported commands:\n\t ls, get <filename>, sharedir <sharedirectory>, chat <textmessage to peer>, quit");
				System.out.println("examples:\n\t * change your local shared directory with \"sharedir c:\\shared\\\"" +
						"\n\t * list the files available from your peer with \"ls\"" +
						"\n\t * get a file with \"get file1.jar\" or even \"get filename with spaces.txt\"");
			}
				
		}
	}
}
